package ltd.followkba.comment.utils;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.List;

import ltd.followkba.comment.MBAPP;
import ltd.followkba.comment.MKCS;
import main.MAPI;

/**
 * Created by Administrator on 2018/4/8.
 */

public class InfoFile {
    public String fileName;
    public InfoFile(int AppType, String fileType) {
        this.fileName = MKCS.gsTaskPath[AppType] + fileType;


    }
    public void DeleteAllInfoID() {
//MBAPP.MSTRING("DeleteAllInfoID "+fileName);
        MAPI.DELETE_FILE(fileName);
    }
    public int GetSelectIndex() {
        int g4Local_1 = MBAPP.RD_PRIVATE_VALUE("select", fileName);
        int itemLength = MBAPP.RD_PRIVATE_VALUE("length", fileName);
        if (g4Local_1 < itemLength){
            return g4Local_1;
        }
        return 0;
    }
    public String GetSelectInfoID() {
        int g4Local_1 = GetSelectIndex();
        return MBAPP.RD_PRIVATE_STRING(g4Local_1, fileName);
    }
    public String GetSpecInfoID(int index) {
        return MBAPP.RD_PRIVATE_STRING(index, fileName);
    }
    public void WrPrivateString(String key, String string) {
        MBAPP.WR_PRIVATE_STRING(key, string, fileName);
    }
    public String RdPrivateString(String key) {
        return MBAPP.RD_PRIVATE_STRING(key, fileName);
    }
    public void WrSelectIndex(int index) {
        MBAPP.WR_PRIVATE_VALUE("select", index, fileName);
    }
    public void WrSelectInfoID(String infoID) {
        String gsLocal_1;
        int g4Local_1 = 0;
        int itemLength = MBAPP.RD_PRIVATE_VALUE("length", fileName);
        while (g4Local_1 < itemLength) {
            gsLocal_1 = MBAPP.RD_PRIVATE_STRING(g4Local_1, fileName);
            if (gsLocal_1.equals(infoID)) {
                WrSelectIndex(g4Local_1);
                break;
            }
            ++g4Local_1;
        }
    }
    public int DeleteInfo(String infoID) {
        String gsLocal_1;
        int g4Local_1 = 0;
        int g4Local_2;
        int itemLength = MBAPP.RD_PRIVATE_VALUE("length", fileName);
        while (g4Local_1 < itemLength) {
            gsLocal_1 = MBAPP.RD_PRIVATE_STRING(g4Local_1, fileName);
            if (gsLocal_1.equals(infoID)) {
                if ((g4Local_1+1) == itemLength){   // last
                    MBAPP.WR_PRIVATE_STRING(g4Local_1, "", fileName);
                    g4Local_2 = g4Local_1;
                    if (g4Local_1 > 0){
                        g4Local_1 -= 1;
                    }
                    MBAPP.WR_PRIVATE_VALUE("select", g4Local_1, fileName);
                    itemLength -= 1;
                    MBAPP.WR_PRIVATE_VALUE("length", itemLength, fileName);
                    return g4Local_2;
                }
                else {
                    break;
                }
            }
            ++g4Local_1;
        }
        g4Local_2 = g4Local_1;
        while (g4Local_1 < itemLength) {
            gsLocal_1 = MBAPP.RD_PRIVATE_STRING(g4Local_1+1, fileName);
            if ((g4Local_1+1) == itemLength) {
                gsLocal_1 = "";
            }
            MBAPP.WR_PRIVATE_STRING(g4Local_1, gsLocal_1, fileName);
            ++g4Local_1;
        }
        itemLength -= 1;
        MBAPP.WR_PRIVATE_VALUE("length", itemLength, fileName);
        return g4Local_2;
    }
    public void AddInfoToFile(Object object, String infoID){
        int itemLength = MBAPP.RD_PRIVATE_VALUE("length", fileName);
        int g4Local_1 = 0;
        String gsLocal_1;
        String gsLocal_2 = ObjectToString(object);
        while (g4Local_1 < itemLength){
            gsLocal_1 = MBAPP.RD_PRIVATE_STRING(g4Local_1, fileName);
            if (gsLocal_1.equals(infoID)) {
                MBAPP.WR_PRIVATE_STRING("F" + infoID, gsLocal_2, fileName);
                return;
            }
            ++g4Local_1;
        }
        MBAPP.WR_PRIVATE_STRING(itemLength, infoID, fileName);
        MBAPP.WR_PRIVATE_STRING("F" + infoID, gsLocal_2, fileName);
        itemLength += 1;
        MBAPP.WR_PRIVATE_VALUE("length", itemLength, fileName);
    }
    public Object GetInfoFromFile(String infoID){
        String gsLocal_1;
        String gsLocal_2;
        int g4Local_1 = 0;
        int itemLength = MBAPP.RD_PRIVATE_VALUE("length", fileName);
        while (g4Local_1 < itemLength) {
            gsLocal_1 = MBAPP.RD_PRIVATE_STRING(g4Local_1, fileName);
            if (gsLocal_1.equals(infoID)) {
//MBAPP.MSTRING("GetInfoFromFile "+itemLength+" "+gsLocal_1);
                gsLocal_2 = MBAPP.RD_PRIVATE_STRING("F" + gsLocal_1, fileName);
                Object object = StringToObject(gsLocal_2);
//                if (object != null && object instanceof AUserInfo) {
                return object;
//                }
            }
            ++g4Local_1;
        }
//MBAPP.MSTRING("GetInfoFromFile 2 "+aUserLists.size());
        return null;
    }
    public List<Object> GetInfoFromFile(int length){
        List<Object> objects = new ArrayList<>();
        String gsLocal_1;
        String gsLocal_2;
        int g4Local_1 = 0;
        int itemLength = MBAPP.RD_PRIVATE_VALUE("length", fileName);
        if (length > 0){
            itemLength = (itemLength > length) ? length : itemLength;
        }
//MSTRING("GetInfoFromFile A "+itemLength+" "+length+" "+fileName);
        while (g4Local_1 < itemLength) {
            gsLocal_1 = MBAPP.RD_PRIVATE_STRING(g4Local_1, fileName);
//MBAPP.MSTRING("GetInfoFromFile "+itemLength+" "+gsLocal_1);
            gsLocal_2 = MBAPP.RD_PRIVATE_STRING("F" + gsLocal_1, fileName);
            Object object = StringToObject(gsLocal_2);
//            if (object != null && object instanceof AUserInfo) {
            objects.add(object);
//            }
            ++g4Local_1;
        }
//MBAPP.MSTRING("GetInfoFromFile 2 "+aUserLists.size());
        return objects;
    }
    public String ObjectToString(Object object){
        byte[] bytes = null;
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        try {
            ObjectOutputStream oos = new ObjectOutputStream(bos);

            oos.writeObject(object);

            oos.flush();
            bytes = bos.toByteArray ();
            oos.close();
            bos.close();


//            ByteArrayOutputStream bos = new ByteArrayOutputStream();
//            ObjectOutputStream os = new ObjectOutputStream(bos);
////            MBAPP.MSTRING("1111111");
//            //将对象序列化写入byte缓存
//            os.writeObject(object);
////            MBAPP.MSTRING("1113333");

        } catch (IOException ex) {
            ex.printStackTrace();
        }
//        MBAPP.MSTRING("17711");
        if (bytes != null) {
//            MBAPP.MSTRING("3366111");
            return CodeBase64.encodeBytes(bytes);
        }
        return "";
    }
    public Object StringToObject(String string){
        byte[] bytes = null;
        try {
            bytes  = CodeBase64.decode(string);
        } catch (IOException e) {
            e.printStackTrace();
        }
        Object object = null;
        try {
            ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
            ObjectInputStream ois = new ObjectInputStream(bis);
            object = ois.readObject();
            ois.close();
            bis.close();


//            ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
//            ObjectInputStream is = new ObjectInputStream(bis);
//            //返回反序列化得到的对象
//            object = is.readObject();
        } catch (IOException ex) {
            ex.printStackTrace();
        } catch (ClassNotFoundException ex) {
            ex.printStackTrace();
        }
        return object;
    }
}
